<!--
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 14:41:20
 * @LastEditTime: 2023-09-21 16:04:16
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description:   :gridOps="gridOptions" 
-->
<template>
  <el-container style="height: 100%">
    <el-aside width="150px">
      <sk-category :config="{
        dicList: [
          { id: 1, name: '资产类', children: [] },
          { id: 2, name: '负债类', children: [] },
          { id: 3, name: '所有者权益', children: [] },
          { id: 4, name: '成本类', children: [] },
          { id: 5, name: '损益类', children: [] },
        ]
      }" :editable="false" @select="categoryselect"></sk-category>
    </el-aside>
    <el-main style="height: 100%">
      <sc-VxeTable ref="xGrid1" :gridEvts="gridEvents" :tablecode="'finance_initialization'" @editClosed="editClosed"
        :url="this.$API.finance.initialization.list">
        >
      </sc-VxeTable>
    </el-main>
    <company v-if="dialog.company" ref="companydialog" @success="handleSuccess" :urlParams="urlParams"
      @closed="dialog.company = false">
    </company>
    <department v-if="dialog.department" ref="departmentdialog" @success="handleSuccess" :urlParams="urlParams"
      @closed="dialog.department = false">
    </department>
    <material v-if="dialog.material" ref="materialdialog" @success="handleSuccess" :urlParams="urlParams"
      @closed="dialog.material = false">
    </material>
  </el-container>
</template>
<script>

import skCategory from "@/components/skCategory";
import department from "./department";
import company from "./company";
import material from "./material";
export default {
  components: {
    company, skCategory, department, material
  },
  name: "finance_initialization",
  data() {
    return {
      dialog: {
        company: false,
        department: false,
        material: false,
      },
      urlParams: "",
      gridEvents: {
        cellDblclick: ({ row }) => {
          this.edit(row);
        },

        edit: () => {
          let selects = this.$refs.xGrid1.xGrid.getCheckboxRecords();
          if (selects.length == 0) {
            this.$message.error("必须选中元素");
            return;
          }
          this.edit(selects[0]);
        },
        //控制是否可以编辑
        beforeEditMethod: ({ row }) => {
          return row.accounting == "" && row.children.length === 0;
        }
      },
    };
  },
  methods: {
    editClosed({ row, column }) {
      const xGrid = this.$refs.xGrid1.xGrid
      if (xGrid) {
        const field = column.field
        const cellValue = row[field]
        // 判断单元格值是否被修改
        if (xGrid.isUpdateByRow(row, field)) {
          this.$API.finance.initialization.save.post({ amount: cellValue, subject_id: row['id'], id: row['initialization_id'] }).then((res) => {
            if (res.code != 200) {
              row[field] = "";
            }
            else {
              this.$message.success("操作成功");
            }
            xGrid.reloadRow(row, null, field);
          });

        }
      }
    },
    categoryselect: function (data, ids) {
      this.$refs.xGrid1.search("category_id", ids, "in");
      this.handleSuccess();
    },
    handleSuccess() {
      this.$refs.xGrid1.refresh();
    },
    edit(item) {
      let dialog = null;
      if (item.children.length != 0) {
        return this.$message.error("不允许在父科目操作！");
      }
      let editname = "";
      if (item.accounting.indexOf("2") > 0)
        editname = "company";
      if (item.accounting.indexOf("1") > 0)
        editname = "department";
      if (item.accounting.indexOf("4") > 0)
        editname = "material";

      this.urlParams = { 'subject_id': item.id + '' };
      this.dialog[editname] = true;
      this.$nextTick(() => {
        dialog = this.$refs[editname + 'dialog'];
        if (dialog)
          dialog.open();
      });
    }
  },
};
</script>
